package io.wdb.wx.activity.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import io.wdb.common.exception.RRException;
import io.wdb.common.utils.R;
import io.wdb.modules.activity.wx.entity.MarketPrizeEntity;
import io.wdb.modules.activity.wx.entity.UserChannelInfoEntity;
import io.wdb.wx.activity.logic.PrizeLogic;
import io.wdb.wx.message.resp.BaseResponse;
import io.wdb.wx.message.resp.prize.PrizeNumVO;
import io.wdb.wx.message.resp.prize.PrizeVO;
import io.wdb.wx.message.resp.prize.UserPrizeVO;
import io.wdb.wx.service.PrizeService;

/**
 * 微信抽奖 
 */
@RestController
@RequestMapping("wx")
@SuppressWarnings({"unchecked","rawtypes"})
public class LoterryController extends BaseController{
	
	private static Logger logger = LoggerFactory.getLogger(LoterryController.class);
	
	@Resource(name="prizeServiceImpl")
	private PrizeService prizeService;
	
	@Resource(name="prizeLogic")
	private PrizeLogic prizeLogic;
	
	/**
	 * @param openId
	 * @return
	 */
	@RequestMapping(value = "loterry")
	public Map<String, Object> getUrl(String openId) {
		Map<String, Object> result = new HashMap<>();
		result.put("openId", openId);
		int times = 3;
		result.put("times", times);
		return R.ok(result);
	}
	/**
	 * 
	 * @Method:getPrizeIntegral
	 * @Description:获取用户当前积分以及用户可抽奖次数
	 * @Time 2017年11月23日 上午11:46:35
	 * @author: Tom
	 * @param user_id
	 * @return
	 */
	@RequestMapping(value = "getPrizeIntegral",method=RequestMethod.POST)
	public BaseResponse getPrizeIntegral(Long user_id){
		logger.info("获取用户当前积分以及用户可抽奖次数接口接收到的前端数据为：\t"+user_id);
		BaseResponse baseResponse = success();
		PrizeNumVO prizeNumVO = new PrizeNumVO();
		try {
			//查看用户是否存在
			UserChannelInfoEntity channelInfoEntity = prizeService.getUserInfo(user_id);
			if(channelInfoEntity == null) {
				throw new RRException("正在加载用户信息...");
			}
			prizeNumVO = prizeService.getUserIntegral(user_id);
			prizeNumVO.setUser_id(user_id);
			prizeNumVO.setUser_img(channelInfoEntity.getChannelImg());
			prizeNumVO.setUser_nick_name(channelInfoEntity.getChannelNickName());
			baseResponse.setDetail(prizeNumVO);
		}catch(Exception e) {
			logger.error(e.getMessage(),e);
			baseResponse=failed(e);
		}
		logger.info("获取用户当前积分以及用户可抽奖次数接口返回给前端的数据为,{}"+JSON.toJSONString(prizeNumVO));
		return baseResponse;
	}
	/**
	 * 
	 * @Method:submitPrize
	 * @Description:客户抽奖信息返回
	 * @Time 2017年11月23日 下午2:17:26
	 * @author: Tom
	 * @return 
	 */
	@RequestMapping(value = "submitPrize",method=RequestMethod.POST)
	public BaseResponse submitPrize(Long user_id) {
		logger.info("客户抽奖信息返回接口接收到的前端数据为：\t"+user_id);
		BaseResponse baseResponse = success();
		UserPrizeVO userPrizeVO = new UserPrizeVO();
		try {
			//查看用户是否存在
			UserChannelInfoEntity channelInfoEntity = prizeService.getUserInfo(user_id);
			if(channelInfoEntity == null) {
				throw new RRException("正在加载用户信息...");
			}
			//再次查看用户的可用抽奖次数是否已经用完
			PrizeNumVO prizeNumVO = prizeService.getUserIntegral(user_id);
			if(prizeNumVO.getUser_prize_num()<1) {
				//返回用户次数已经用完
				logger.info("客户当前积分不能满足抽奖一次，请获取到足够积分后再进行");
				userPrizeVO.setStatus_code(101);
				userPrizeVO.setStatus_message("感谢您参与21天亲子亲子社群活动，您本期积分已不足，敬请期待下期活动获得积分哦");
				baseResponse.setDetail(userPrizeVO);
				return baseResponse;
				//throw new RRException("客户当前积分不能满足抽奖一次，请获取到足够积分后再进行");
			}
			//开始抽奖
			MarketPrizeEntity marketPrizeEntity = prizeLogic.getPrize();
			prizeService.submietUserPrize(channelInfoEntity, marketPrizeEntity);
			String prizeAngle = prizeLogic.getAngle(marketPrizeEntity.getPrizeSite());
			userPrizeVO.setPrize_name(marketPrizeEntity.getPrizeName()+marketPrizeEntity.getPrizeNamePart());
			userPrizeVO.setPrize_dec(marketPrizeEntity.getPrizeDesc());
			userPrizeVO.setPrize_angle(prizeAngle);
			userPrizeVO.setUser_id(user_id);
			baseResponse.setDetail(userPrizeVO);
		}catch(Exception e) {
			logger.error(e.getMessage(),e);
			baseResponse = failed(e);
		}
		logger.info("获取用户当前积分以及用户可抽奖次数接口返回给前端的数据为,{}"+JSON.toJSONString(userPrizeVO));
		return baseResponse;
	}
	
	/**
	 * 
	 * @Method:getAllPrizes
	 * @Description:获取当期所有奖品
	 * @Time 2017年11月23日 下午5:37:40
	 * @author: Tom
	 */
	@RequestMapping(value = "getAllPrizes",method=RequestMethod.POST)
	public BaseResponse getAllPrizes() {
		BaseResponse baseResponse = success();
		try {
			List<MarketPrizeEntity> marketPrizeEntities = prizeService.getAllPrizes();
			List<PrizeVO> prizeVOs = new ArrayList<PrizeVO>();
			for(MarketPrizeEntity marketPrizeEntity:marketPrizeEntities) {
				PrizeVO prizeVO = new PrizeVO();
				prizeVO.setPrize_name(marketPrizeEntity.getPrizeName());
				prizeVO.setPrize_site(marketPrizeEntity.getPrizeSite());
				//String prizeNamePart = prizeLogic.getAngle(marketPrizeEntity.getPrizeSite());
				prizeVO.setPrize_name_part(marketPrizeEntity.getPrizeNamePart());
				prizeVOs.add(prizeVO);
			}
			baseResponse.setItems(prizeVOs);
		}catch(Exception e) {
			logger.error(e.getMessage(),e);
			baseResponse = failed(e);
		}
		return baseResponse;
	}
}
